from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from tqdm import tqdm
import csv

# 设置Chrome选项
chrome_options = Options()
chrome_options.add_argument("--headless")  # 无头模式，不打开浏览器界面
chrome_options.add_argument("--disable-gpu")

# 设置 Selenium WebDriver
driver = webdriver.Chrome(options=chrome_options)

# 打开网页
url = "https://athena.ohdsi.org/search-terms/terms/6"  
driver.get(url)

# 等待页面加载（隐式等待，更快一点）
driver.implicitly_wait(10)

# 定位表格中的行
rows = driver.find_elements(By.CSS_SELECTOR, "li.ac-list-item")

# 初始化数据字典
data_dict = {}

# tqdm 可视化进度条
for row in tqdm(rows, desc="Extracting Table Data", unit="row"):
    try:
        # 提取键和值
        key = row.find_element(By.CLASS_NAME, 'at-term__attribute-name').text.strip()
        for i in row.find_elements(By.TAG_NAME, 'span'):
            print(i.text)
        print('提取的值为'+row.find_elements(By.TAG_NAME, 'span')[-2].text)
        value = row.find_elements(By.TAG_NAME, 'span')[-2].text.strip()

        data_dict[key] = value  # 将键和值存入字典
    except Exception as e:
        print(f"跳过一行，错误: {e}")

# 转置数据（每个键为一列）
output_csv = 'output.csv'
with open(output_csv, 'a', newline='', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile)

    # 写入列标题
    writer.writerow(data_dict.keys())

    # 写入单行数据
    writer.writerow(data_dict.values())

print(f"提取的数据已保存到 {'output.csv'}")
